/**
* Author: CZ (cz.devnet@gmail.com)
*
* Description:
 */

package main

import (
	"fmt"
	"net/http"
	"time"
)

func init() {

}

type Claw struct {
	filter *Filter
	bag    *Bag
	URLs   []string
}

func NewClaw() *Claw {
	claw := &Claw{}
	claw.init()
	return claw
}

func (c *Claw) init() {
	c.filter = NewFilter()
	c.bag = NewBag()
}

func (c *Claw) AddURL(URL string) {
	c.URLs = append(c.URLs, URL)
}

func (c *Claw) Grab(timeout time.Duration) {
	for _, URL := range c.URLs {
		c.grab(URL, timeout)
	}
}

func (c *Claw) grab(URL string, timeout time.Duration) {
	fmt.Printf("Now grabing URL[%s] with Timeout[%v]\n", URL, timeout)
	client := &http.Client{}
	client.Timeout = timeout
	rsp, err := client.Get(URL)
	if nil != err {
		fmt.Printf("Get Error:%s\n", err.Error())
		return
	}
	rst := c.filter.Filter(rsp)
	c.bag.Freight(rst)
}
